//
// Created by YimingCai on 26/03/2017.
//
#include <string>
using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if (s.empty()) return 0;
        int maxLen = 1;
        for (string::size_type first = 0, tail = 1; tail < s.length(); ++tail) {
            for (string::size_type i = first; i < tail; ++i) {
                if (s.at(i) == s.at(tail)) {
                    first = i + 1;
                    break;
                }
            }
            if (tail - first + 1 > maxLen) {
                maxLen = static_cast<int>(tail - first + 1);
            }
        }
        return maxLen;
    }
};

#include <gtest/gtest.h>
TEST(LengestSubstringTest, test1)
{
    Solution s = Solution();
    EXPECT_EQ(3, s.lengthOfLongestSubstring("abcabcbb"));
    EXPECT_EQ(1, s.lengthOfLongestSubstring("bbbbbb"));
    EXPECT_EQ(3, s.lengthOfLongestSubstring("pwwkew"));
    EXPECT_EQ(5, s.lengthOfLongestSubstring("abcde"));
}

int main(int argc, char** argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}
